<template>
	<view class="">
		<nav-tab :navIndex="1"></nav-tab>
		<!-- 广告插入 -->
		<adview :title="title" @adfun="adfun"></adview>
		<view class="" v-if="!adShow">
			<u-sticky>
				<view class="">
					<view class="bg-white">
						<view class="px-2 py-1"><search-view @search="searchChange" @clear="searchChange('')"></search-view></view>
					</view>
					<view class="home_top bg-white rounded_15 "><tab :tab-list="productCate" @tabAll="tabAll" @contTabChange="contTabChange"></tab></view>
				</view>
			</u-sticky>
			<view class=" px-2" v-if="companylists.length == 0">
				<view class="p-2 mt-3"><u-empty mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png"></u-empty></view>
			</view>
			<view v-else class="list_warp px-2">
				<company-list v-if="companylists.length >= 1" :exhibition-id="exhibitionId" :list="companylists" :check-types="checkTypes"></company-list>
				<u-loadmore :status="status" color="#E5E5E5" lineColor="#E5E5E5" dashed line />
			</view>
			<u-back-top :scroll-top="scrollTop"></u-back-top>
		</view>
	</view>
</template>

<script>
import navTab from '@/components/nav-tab/nav-tab.vue';
import { getProductCate, getCompanyList } from '@/api/home/company.js';
import searchView from '@/components/search/search.vue';
import tab from '@/components/tab/index.vue';
import companyList from '@/components/company_list.vue';
import adview from '@/components/adview/adview.vue';
export default {
	components: { navTab, searchView, tab, companyList, adview },
	data() {
		return {
			searchV: '',
			productCate: [], //企业分类
			contTabCur: -1,
			companylists: [], //企业列表
			status: 'loadmore',
			page: 1, //页数
			catId: '',
			suo: true,
			scrollTop: 0,
			checkTypes: 'qy', //类型 qy企业 cp产品
			adShow: true
		};
	},
	onPageScroll(e) {
		this.scrollTop = e.scrollTop;
	},
	onLoad() {
		this.getInfo();
		// 分享
		wx.showShareMenu({
			withShareTicket: true,
			menus: ['shareAppMessage', 'shareTimeline']
		});
	},
	onShow() {
		this.reset();
		this.companyList();
	},
	onReachBottom() {
		if (this.suo) {
			//定义个小锁，当没有数据时锁住suo
			this.status = 'loading';
			this.page = ++this.page;
			setTimeout(() => {
				if (this.catId) {
					this.companyList(this.catId);
				} else {
					this.companyList();
				}
			}, 1000);
		}
	},
	methods: {
		// 广告页
		adfun(val) {
			this.adShow = val;
		},
		reset() {
			this.companylists = [];
			this.page = 1;
		},
		searchChange(value) {
			this.searchV = value;
			this.companyList();
			this.reset();
		},
		// tab-全部
		tabAll() {
			this.contTabCur = -1;
			this.catId = '';
			this.reset();
			this.companyList();
			this.homeScroll();
		},
		contTabChange(e) {
			this.contTabCur = e.idx;
			this.catId = e.val.id;
			this.reset();
			this.companyList(e.val.id);
			this.homeScroll();
		},
		homeScroll() {
			setTimeout(() => {
				uni.pageScrollTo({
					duration: 300,
					scrollTop: this.topHeight //滚动高度，单位px，这里也可用选择器属性:selector:选择器
				});
			}, 50);
		},
		getInfo() {
			// 获取企业分类
			getProductCate().then(res => {
				// console.log(res);
				if (res.code == 0) {
					this.productCate = res.data;
					// this.reset();
					// this.companyList(this.productCate[0].id);
				} else {
					uni.$u.toast(res.msg);
				}
			});
		},
		// 企业列表
		companyList(val) {
			let msg = {
				id: this.exhibitionId,
				kw: decodeURIComponent(this.searchV) || '',
				page: this.page,
				catid: val,
				uid: uni.getStorageSync('userInfo').userId || '',
				utype: 2
			};
			getCompanyList(msg).then(res => {
				this.companylists.push(...res.data.list);
				if (res.data.list.length == 0) {
					this.status = 'nomore';
					this.suo = false;
				} else if (res.data.list.length < 10) {
					this.status = 'nomore';
					this.suo = false;
				} else {
					this.status = 'loadmore';
					this.suo = true;
				}
			});
		}
	}
};
</script>

<style></style>
